//
//  QMANetworkChannelDelegateImp.m
//  QQMiniAppSDK
//
//  Created by raymonddeng on 2021/1/28.
//

#import "QMANetworkChannelDelegateImp.h"

@implementation QMANetworkChannelDelegateImp

- (BOOL)sendRequest:(id<QQNetworkObjectProtocol>)request
        withReqData:(NSData *)reqData
          contextID:(nonnull int64_t *)contextID
         parentSpan:(nullable QQOTSpan *)parentSpan
  completionHandler:(void (^)(NSData *, NSError *))completionHandler __attribute__((annotate("oclint:suppress"))) {
    if (![[QQMiniAppSDKManager sharedInstance].sdkDelegate respondsToSelector:@selector(getPlatformId)]) {
        assert(0 && "-[QMASDKDelegate getPlatformId] 未实现");
    }
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSString *urlString = [QQMiniAppSDKManager miniAppRequestURLString];
    
    NSMutableURLRequest *httpRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    httpRequest.HTTPMethod = @"POST";
    httpRequest.HTTPBody = reqData;
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:httpRequest
                                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (completionHandler) {
            completionHandler(data, error);
        }
    }];
    
    [dataTask resume];
    
    return YES;
}

@end
